package com.guli.eduService.controller;


import com.guli.commonutils.R;
import com.guli.eduService.client.VodClient;
import com.guli.eduService.entity.EduChapter;
import com.guli.eduService.entity.EduVideo;
import com.guli.eduService.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author LMZovo
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;

    @ApiOperation(value = "添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    @ApiOperation(value = "删除小节同时删除视频")
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable("id") String id) {
        //顺序有要求，先删除视频再删除小节
        //获取视频id
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //根据视频id，远程调用实现视频的删除
        if (!StringUtils.isEmpty(videoSourceId)) {
            vodClient.removeAlyVideo(videoSourceId);
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }


    @ApiOperation(value = "根据id查询")
    @GetMapping("{courseId}")
    public R getChapterInfo(@PathVariable("courseId") String courseId) {
        EduVideo eduVideo = videoService.getById(courseId);
        return R.ok().data("eduVideo", eduVideo);
    }


    @ApiOperation(value = "修改小节")
    @GetMapping("updateChapter")
    public R updateChapter(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return R.ok();
    }


}